import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:"/",
      redirect:"/login"
    },
    {
      path: '/login',
      name: 'login',
      component: ()=>import("../views/Login.vue"),
      //    beforeEnter(to, from, next) {
      //   if (localStorage.getItem("Authorization")) {
      //     next("/dashboard");
      //   } else {
      //     next("/login");
      //   }
      // },
    },
    {
      path:"/dashboard",
      redirect:"/dashboard/welcome"
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: () => import('../views/dashboard.vue'),
      children:[
        {
          path:"welcome",
          name:"welcome",
          component:()=>import ("../views/dashboard/welcome.vue")
        },
        {
          path:"user",
          name:"user",
          component:()=>import("../views/user/user.vue"),
          children:[
            {
              path:"index",
              name:"index",
              component:()=>import("../views/user/userlist.vue")
            },
            {
              path:"add",
              name:"add",
              component:()=>import("../views/user/adduser.vue")
            },
            {
              path:"del",
              name:"del",
              component:()=>import("../views/user/deluser.vue")
            },
            {
              path:"mod",
              name:"mod",
              component:()=>import("../views/user/chanuser.vue")
            },
          ]
        },
        {
          path:"film",
          name:"film",
          component:()=>import("../views/move/moveview.vue"),
          children:[
            {
              path:"index",
              name:"index2",
              component:()=>import("../views/move/movelist.vue")
            },
            {
              path:"add",
              name:"add2",
              component:()=>import("../views/move/adduser.vue")
            },
            {
              path:"del",
              name:"del2",
              component:()=>import("../views/move/deluser.vue")
            },
            {
              path:"mod",
              name:"mod2",
              component:()=>import("../views/move/chanuser.vue")
            },
          ]
        },
        {
          path:"cinema",
          name:"cinema",
          component:()=>import("../views/map/mapview.vue"),
          children:[
            {
              path:"index",
              name:"index3",
              component:()=>import("../views/map/index.vue")
            },
            // {
            //   path:"add",
            //   name:"add2",
            //   component:()=>import("../views/move/adduser.vue")
            // },
            // {
            //   path:"del",
            //   name:"del2",
            //   component:()=>import("../views/move/deluser.vue")
            // },
            // {
            //   path:"mod",
            //   name:"mod2",
            //   component:()=>import("../views/move/chanuser.vue")
            // },
          ]
        }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
       //如果是登录页可以跳转
       if (to.fullPath === "/login") {
        next();
      } else if (localStorage.getItem("Authorization")) {
        //有token就是登录过了
        next();
      } else {
        //如果没有登录过就去登录页
        router.push("/login");
      }
})
export default router
